package com.study.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.study.services.QaService;

@Controller
public class QAController {
	@Autowired
	QaService service;
	

	/**
	 * 分页+全部+带条件查询 ty
	 * 
	 * @return
	 */
	@PostMapping("/afterSale/kehuQa")
	@ResponseBody
	public String getForm3(String input, String selects, int currentPage, int pagesize) {
		List<JSONObject> list = service.selectBySel(currentPage, pagesize, input.trim(), selects.trim());
		PageInfo<JSONObject> pager = new PageInfo<JSONObject>(list);
		String json = JSONObject.toJSONString(pager);
		return json;
	}
	
	@GetMapping("/qa2")
	public ModelAndView getForm2() {
		ModelAndView mv = new ModelAndView("/afterSale/addQa");
		mv.addObject("qa", new JSONObject());
		return mv;
	}
	
	/**
	 * 添加
	 */
	@PostMapping("qaform")
	@ResponseBody
	public boolean handleForm(String theme,String anwser,String type) {
		boolean result = service.addService(theme,anwser,type);

		return result;
	}
	
	/**
	 * 编辑
	 * @param id
	 * @param theme
	 * @param anwser
	 * @param type
	 * @return
	 */
	@PostMapping("/editerQaform")
	@ResponseBody
	public boolean editorForm(String id,String theme,String anwser,String type) {
		
		boolean result = service.editorQa(id,theme,anwser,type);
		System.out.println(result + "我是售后QA的结果");
		return result;
	}
	
	/**
	 * 编辑
	 */
	@GetMapping("qaEditor")
	public ModelAndView addCustomerByColle(HttpServletRequest request) {
		String parameter = request.getParameter("id");
		JSONObject qa = service.selectBycid(parameter);
		ModelAndView mv = new ModelAndView("/afterSale/addQa");
		mv.addObject("qa", qa);
		return mv;
	}
	
	/**
	 * 删除服务
	 * 
	 * @param 
	 * @return
	 */
	@PostMapping("/delQa")
	@ResponseBody
	public boolean delete(Integer id) {
		boolean result = service.delete(id);
		return result;
	}
	
	/**
	 * 根据名字模糊查询QA问题
	 */
	@PostMapping("/queryMesssageQa")
	@ResponseBody
	public List<JSONObject> queryByNameLike(String msg){
		List<JSONObject> qa = service.findByLike(msg);
		return qa;
	}
	
	/**
	 * 添加次数
	 */
	@PostMapping("/addQaCount")
	@ResponseBody
	public boolean addQaCount(String qaid) {
		return service.addQaCount(qaid);
	}


}
